﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using KNS.Invoice.UI;
using InvoiceManagement.Controllers;
using DAL;
using BLL;
using DAL.Linq;

namespace InvoiceManagement
{
    public partial class frmLogin : KNSFormBase
    {
        #region "Khai báo biến"

        #endregion

        #region "Các hàm load dữ liệu"
        public frmLogin()
        {
            InitializeComponent();
        }
        #endregion        

        #region "Các hàm sự kiện trên form"
        private void frmLogin_Load(object sender, EventArgs e)
        {
            LoadDateTime();
            txtUserName.Focus();            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            GetLogin();    
        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmConnect frm = new frmConnect();
            frm.Show();
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                GetLogin();
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                GetLogin();
            }
        }
        #endregion

        #region "Các hàm xử lý khác"
        public void LoadDateTime()
        {
            string lblNgay = "";
            string lblThang = "";
            string lblGio = "";
            string lblPhut = "";
            string lblGiay = "";

            if (DateTime.Now.Day.ToString().Length == 1)
            {
                lblNgay = "0" + DateTime.Now.Day.ToString();
            }
            else
            {
                lblNgay = DateTime.Now.Day.ToString();
            }

            if (DateTime.Now.Month.ToString().Length == 1)
            {
                lblThang = "0" + DateTime.Now.Month.ToString();
            }
            else
            {
                lblThang = DateTime.Now.Month.ToString();
            }

            if (DateTime.Now.Hour.ToString().Length == 1)
            {
                lblGio = "0" + DateTime.Now.Hour.ToString();
            }
            else
            {
                lblGio = DateTime.Now.Hour.ToString();
            }

            if (DateTime.Now.Minute.ToString().Length == 1)
            {
                lblPhut = "0" + DateTime.Now.Minute.ToString();
            }
            else
            {
                lblPhut = DateTime.Now.Minute.ToString();
            }

            if (DateTime.Now.Second.ToString().Length == 1)
            {
                lblGiay = "0" + DateTime.Now.Second.ToString();
            }
            else
            {
                lblGiay = DateTime.Now.Second.ToString();
            }

            lblDateTime.Text = lblNgay + "/" + lblThang + "/" + DateTime.Now.Year.ToString() + " - " + lblGio + ":" + lblPhut + ":" + lblGiay;
        
        }

        public void GetLogin()
        {
            try
            {
                if (txtUserName.Text == string.Empty || txtUserName.Text == "")
                {
                    KNSMessageBox.Show("Chưa nhập tên đăng nhập", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    txtUserName.Focus();
                }
                else
                {
                    SYS_USER lst = null;
                    SYS_USERController sys_User = new SYS_USERController();
                    lst = sys_User.GetLogin(txtUserName.Text.Trim().ToString(), txtPassword.Text.Trim().ToString());
                    if (lst != null)
                    {
                        frmMain frm = new frmMain();
                        this.Hide();
                        frm.Show();
                        //this.UId = lst.UserID;                        
                        clsCommon.uID = lst.UserID;
                    }
                    else
                    {
                        KNSMessageBox.Show("Đăng nhập không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                }                
            }
            catch { }
        }
        #endregion                                     
    }
}